package com.yyn.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyn.R;
import com.yyn.order.client.EduClient;
import com.yyn.order.client.UcenterClient;
import com.yyn.order.entity.TOrder;
import com.yyn.order.service.TOrderService;
import com.yyn.order.utils.OrderNoUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author yyn
 * @since 2023-01-12
 */
@RestController
@CrossOrigin
@RequestMapping("/order/t-order")
public class TOrderController {
    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;
@Autowired
private TOrderService tOrderService;
    @ApiOperation("获取课程信息")
    @GetMapping("getCourseInfo{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        return eduClient.getInfoById(courseId);
    }

    @ApiOperation("直接通过token获取用户信息")
    @GetMapping("getUcenterInfo{token}")
    public R getUcenterInfo(@PathVariable String token) {
        return ucenterClient.getMemberInfoByToken(token);
    }

    @ApiOperation("整合课程与用户信息-订单信息")
    @GetMapping("getOrderInfo/{courseId}/{token}")
    public R getOrderInfo(@PathVariable String courseId, @PathVariable String token) {
        R courseInfo = this.getCourseInfo(courseId);
        R ucenterInfo = getUcenterInfo(token);
        HashMap courseObj = (HashMap) courseInfo.getData().get("infoById");
        HashMap<String, String> memberObj = (HashMap<String, String>) ucenterInfo.getData().get("memberInfo");
        TOrder tOrder = new TOrder();
        tOrder.setOrderNo(OrderNoUtil.getOrderNo());
        tOrder.setCourseId((String) courseObj.get("id"));
        tOrder.setCourseCover((String) courseObj.get("cover"));
        tOrder.setCourseTitle((String) courseObj.get("title"));
        tOrder.setTeacherName((String) courseObj.get("teacherName"));
        BigDecimal price =BigDecimal.valueOf ((Double) courseObj.get("price"));
        tOrder.setTotalFee(price);
//        multiply(new BigDecimal("100")).longValue()+"")
        tOrder.setMemberId(memberObj.get("id"));
        tOrder.setNickname(memberObj.get("nickname"));
        tOrder.setMobile(memberObj.get("mobile"));
        tOrder.setStatus(0);
        tOrder.setPayType(1);

        tOrderService.save(tOrder);
return R.success().data("tOrder",tOrder);
    }

    @ApiOperation("通过订单号查订单")
    @GetMapping("getOrderInfoByNo/{orderNo}")
    public R getOrderInfoByNo(@PathVariable String orderNo){
        TOrder tOrder=tOrderService.getOrderInfoByNo(orderNo);
        return R.success().data("order",tOrder);
    }
    @ApiOperation("通过用户与课程判断是否购买课程")
    @GetMapping("checkPayCourse/{courseId}/{memberId}")
    public int checkPayCourse(@PathVariable String courseId,@PathVariable String memberId)
    {
        int state=tOrderService.checkPayCourse(courseId,memberId);

        return state;
    }
}

